/**
 * @Author 12629
 * @Date 2022/3/19 10:02
 * @Description：
 */
public class MyCircularQueue {


    public int front;//队头下标
    public int rear;//队尾下标,也是当前可以存放元素的下标
    public int[] elem;

    /**
     * 构造方法
     * @param k 队列的长度
     */
    public MyCircularQueue(int k) {
        this.elem = new int[k];
        //this.elem = new int[k+1];
    }

    /**
     * 入队操作
     * @param value
     * @return
     */
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        this.elem[this.rear] = value;
        this.rear = (this.rear+1)%this.elem.length;//这里不能++，防止越界
        return true;
    }

    /**
     * 出队操作
     * @return
     */
    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        this.front = (this.front+1)%this.elem.length;
        return true;
    }

    /**
     * 获取队头元素
     * @return
     */
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return this.elem[this.front];
    }

    /**
     * 获取队尾元素
     * @return
     */
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        int index = -1;
        if(this.rear == 0) {
            index = this.elem.length-1;
        }else {
            index = this.rear-1;
        }
        return this.elem[index];
    }

    /**
     * 是否为空
     * @return
     */
    public boolean isEmpty() {
        return this.front == this.rear;
    }

    /**
     * 是否为满
     * @return
     */
    public boolean isFull() {
        if((this.rear+1) % this.elem.length == this.front) {
            return true;
        }
        return false;
    }
}
